package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootPersonSkill;
import org.footballmanager.pandafootball.system.form.impl.FootPersonSkillSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootPersonSkillMapper;
import org.footballmanager.pandafootball.system.service.FootPersonSkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootPersonSkillServiceImpl extends ServiceImpl<FootPersonSkill> implements FootPersonSkillService {
    @Autowired
    private FootPersonSkillMapper footPersonSkillMapper;

    /**
     * 根据球员主键查询
     *
     * @param qdId 球员主键
     * @return 技能列表
     */
    @Override
    public List<FootPersonSkill> findByQyid(Long qdId) {
        Example example = new Example(FootPersonSkill.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qyId", qdId);
        criteria.andEqualTo("delFlag", 0);
        example.orderBy("skill_id").asc();
        return this.findall(example);
    }

    /**
     * 根据球员主键和技能名称获取记录
     *
     * @param skillid 技能主键
     * @param qdId    球员主键
     * @return 技能记录
     */
    @Override
    public FootPersonSkill findByNameAndQyid(Long skillid, Long qdId) {
        Example example = new Example(FootPersonSkill.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qdId", qdId);
        criteria.andEqualTo("skillid", skillid);
        criteria.andEqualTo("delFlag", 0);
        example.orderBy("skill_id").desc();
        return this.findByExample(example);
    }

    /**
     * 真实删除记录
     *
     * @param id 主键
     */
    @Override
    public void deleteReal(Long id) {
        baseDao.deleteByPrimaryKey(id);
    }

    /**
     * 查询
     *
     * @param footPersonSkillSearchForm 条件
     * @return 球员技能列表
     */
    @Override
    public List<FootPersonSkill> findall(FootPersonSkillSearchForm footPersonSkillSearchForm) {
        return footPersonSkillMapper.findall(footPersonSkillSearchForm);
    }

    /**
     * 根据主键获取记录
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootPersonSkill get(Serializable id) {
        return footPersonSkillMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname                实体类名
     * @param map                       参数
     * @param footPersonSkillSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootPersonSkillSearchForm footPersonSkillSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footPersonSkillSearchForm);
        Page<FootPersonSkill> page = PageHelper.startPage(footPersonSkillSearchForm.getPageIndex(), footPersonSkillSearchForm.getPageSize());
        List<FootPersonSkill> list = this.findall(footPersonSkillSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footPersonSkillSearchForm 条件
     * @param request                   请求
     */
    @Override
    public void deleteAll(FootPersonSkillSearchForm footPersonSkillSearchForm, HttpServletRequest request) {
        if (footPersonSkillSearchForm.getSelectIds() != null) {
            String[] result = footPersonSkillSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }
}
